# -*-coding:utf-8-*-

import json
import time
import re
import random

from pyrestful import mediatypes
from pyrestful.rest import get, post, put, delete
from bson.objectid import ObjectId

from handlers.base import BaseHandler
from utils.auth import authenticated
from utils.sign import signed
import settings
from alidayu.topnado import Topnado


# POST /platform/api/areas/{aid}/partitions
# PUT  /platform/api/areas/{aid}/partitions/{pid}
# POST /platform/api/areas/{aid}/partitions/{pid}/entrances
# PUT  /platform/api/areas/{aid}/partitions/{pid}/entrances/{eid}
# POST /platform/api/areas/{aid}/partitions/{pid}/rooms
# PUT  /platform/api/areas/{aid}/partitions/{pid}/rooms/{rid}

# dev access token:
# AtHknQ-D364-1dNbrzSvE0U2MUE5Q0I0MTJFREM4Q0JFQUZDMjhDN0U2NUI0M0M3o2slnQsQjiBJT5zQnRny_efEaRymuxzTiLhIksHw8jAwVVMxB1GaVmHv5bZWz0c=

class AreasHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(AreasHandler, self).__init__(application, request, **kwargs)
        self.areas_dao = self.settings['daos']['areas']

    def check_permissions(self, permissions):
        return True

    @get(_path='/platform/api/areas', _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def get_area_list(self):
        result = self.areas_dao.get_area_list(self.user_id)
        self.respond_success(result)

    # TODO: check the auth_code
    @post(_path='/platform/api/areas', _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def create_area(self):
        # TODO: check auth_code alloc by s_admin
        request = json.loads(self.request.body)
        result = self.areas_dao.create_area(self.user_id, request)
        self.respond_success(result)

    @put(_path='/platform/api/areas/{aid}', _type=[str], _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def update_area(self, aid):
        request = json.loads(self.request.body)
        result = self.areas_dao.update_area(self.user_id, aid, request)
        self.respond_success(result)

    @delete(_path='/platform/api/areas/{aid}', _type=[str], _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def delete_area(self, aid):
        result = self.areas_dao.delete_area(self.user_id, aid)
        self.respond_success(result)

    @get(_path='/platform/api/areas/{aid}/partitions',
         _type=[str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def get_partition_list(self, aid):
        result = self.areas_dao.get_partition_list(self.user_id, aid)
        self.respond_success(result)

    @post(_path='/platform/api/areas/{aid}/partitions',
          _type=[str],
          _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def create_partition(self, aid):
        request = json.loads(self.request.body)
        result = self.areas_dao.create_partition(self.user_id, aid, request)
        self.respond_success(result)

    @put(_path='/platform/api/areas/{aid}/partitions/{pid}',
         _type=[str, str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def update_partition(self, aid, pid):
        request = json.loads(self.request.body)
        result = self.areas_dao.update_partition(self.user_id, aid, pid, request)
        self.respond_success(result)

    @delete(_path='/platform/api/areas/{aid}/partitions/{pid}',
            _type=[str, str],
            _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def delete_partition(self, aid, pid):
        result = self.areas_dao.delete_partition(self.user_id, aid, pid)
        self.respond_success(result)

    @get(_path='/platform/api/areas/{aid}/partitions/{pid}/entrances',
         _type=[str, str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def get_entrance_list(self, aid, pid):
        result = self.areas_dao.get_entrance_list(self.user_id, aid, pid)
        self.respond_success(result)

    @post(_path='/platform/api/areas/{aid}/partitions/{pid}/entrances',
          _type=[str, str],
          _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def create_entrance(self, aid, pid):
        request = json.loads(self.request.body)
        result = self.areas_dao.create_entrance(self.user_id, aid, pid, request)
        self.respond_success(result)

    @put(_path='/platform/api/areas/{aid}/partitions/{pid}/entrances/{eid}',
         _type=[str, str, str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def update_entrance(self, aid, pid, eid):
        request = json.loads(self.request.body)
        result = self.areas_dao.update_entrance(self.user_id, aid, pid, eid, request)
        self.respond_success(result)

    @delete(_path='/platform/api/areas/{aid}/partitions/{pid}/entrances/{eid}',
            _type=[str, str, str],
            _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def delete_entrance(self, aid, pid, eid):
        result = self.areas_dao.delete_entrance(self.user_id, aid, pid, eid)
        self.respond_success(result)

    @get(_path='/platform/api/areas/{aid}/partitions/{pid}/rooms',
         _type=[str, str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def get_room_list(self, aid, pid):
        result = self.areas_dao.get_room_list(self.user_id, aid, pid)
        self.respond_success(result)

    @post(_path='/platform/api/areas/{aid}/partitions/{pid}/rooms',
          _type=[str, str],
          _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def create_room(self, aid, pid):
        request = json.loads(self.request.body)
        result = self.areas_dao.create_room(self.user_id, aid, pid, request)
        self.respond_success(result)

    @put(_path='/platform/api/areas/{aid}/partitions/{pid}/rooms/{rid}',
         _type=[str, str, str],
         _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def update_room(self, aid, pid, rid):
        request = json.loads(self.request.body)
        result = self.areas_dao.update_room(self.user_id, aid, pid, rid, request)
        self.respond_success(result)

    @delete(_path='/platform/api/areas/{aid}/partitions/{pid}/rooms/{rid}',
            _type=[str, str, str],
            _produces=mediatypes.APPLICATION_JSON)
    @authenticated
    def delete_room(self, aid, pid, rid):
        result = self.areas_dao.delete_room(self.user_id, aid, pid, rid)
        self.respond_success(result)
